package com.example.word.util;

import androidx.arch.core.util.Function;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CommonUtils {

    // App下载
    public static final String appDownloadUrl = "https://fccs-dsu.oss-cn-beijing.aliyuncs.com/dic/word-ui.apk?OSSAccessKeyId=LTAI4Fynb8gTQzm8hsZnYmBL&Expires=2223419512&Signature=6GdlAD7Fo8GcwgXqmcXh%2F7P9r2E%3D";

    // 获取词汇列表
    public static final String dictUrl = "https://reciteword.youdao.com/reciteword/v1/books";

    // 释义获取：data.entries[].explain
    public static final String wordChineseUrl = "https://dict.youdao.com/suggest?num=1&doctype=json&q=";

    // 例句获取: blng_sents_part.sentence-pair[].sentence sentence-translation
    // 音标获取：simple.word[].usphone
    public static final String wordDetailUrl = "https://dict.youdao.com/jsonapi?q=";

    // 获取音频
    public static final String wordSingUrl = "https://dict.youdao.com/dictvoice?type=1&audio=";


    public static <T, U> int countCommonWords(List<T> list1, List<U> list2, Function<T, String> wordExtractor1, Function<U, String> wordExtractor2) {
        Set<String> wordSet = new HashSet<>();
        for (T obj : list1) {
            wordSet.add(wordExtractor1.apply(obj));
        }

        int commonWordCount = 0;
        for (U obj : list2) {
            if (wordSet.contains(wordExtractor2.apply(obj))) {
                commonWordCount++;
            }
        }

        return commonWordCount;
    }

}
